package com.cirnoworks.utils.text;

import java.text.CharacterIterator;

public class TextFragment implements CharSequence, CharacterIterator {
	public static int MISC_BOLD = 1;
	// public static int MISC_BOLD = 2;
	private float descent = Float.NEGATIVE_INFINITY,
			ascent = Float.NEGATIVE_INFINITY;

	private StringBuilder content;
	private int fontId;
	private int size;
	private int color;
	private int misc;
	private int linkId;

	public float getDescent() {
		return descent;
	}

	public void setDescent(float descent) {
		this.descent = descent;
	}

	public float getAscent() {
		return ascent;
	}

	public void setAscent(float ascent) {
		this.ascent = ascent;
	}

	public TextFragment() {
		content = new StringBuilder();
	}

	public TextFragment(char ch) {
		this();
		content.append(ch);
	}

	public TextFragment(CharSequence seq) {
		content = new StringBuilder(seq);
	}

	public TextFragment(CharSequence seq, int from, int to) {
		content = new StringBuilder(to - from);
		content.append(seq, from, to);
	}

	public CharSequence getContent() {
		return content;
	}

	public void setContent(CharSequence content) {
		descent = ascent = Float.NEGATIVE_INFINITY;
		this.content.setLength(0);
		this.content.append(content);
	}

	public TextFragment append(CharSequence s) {
		descent = ascent = Float.NEGATIVE_INFINITY;
		content.append(s);
		return this;
	}

	public TextFragment append(CharSequence s, int start, int end) {
		descent = ascent = Float.NEGATIVE_INFINITY;
		content.append(s, start, end);
		return this;
	}

	public TextFragment append(char[] str) {
		descent = ascent = Float.NEGATIVE_INFINITY;
		content.append(str);
		return this;
	}

	public TextFragment append(char[] str, int offset, int len) {
		descent = ascent = Float.NEGATIVE_INFINITY;
		content.append(str, offset, len);
		return this;
	}

	public TextFragment append(char c) {
		descent = ascent = Float.NEGATIVE_INFINITY;
		content.append(c);
		return this;
	}

	public void clear() {
		descent = ascent = Float.NEGATIVE_INFINITY;
		content.setLength(0);
	}

	public int getFontId() {
		return fontId;
	}

	public void setFontId(int fontId) {
		this.fontId = fontId;
		descent = ascent = Float.NEGATIVE_INFINITY;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
		descent = ascent = Float.NEGATIVE_INFINITY;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public int getMisc() {
		return misc;
	}

	public void setMisc(int misc) {
		this.misc = misc;
		descent = ascent = Float.NEGATIVE_INFINITY;
	}

	public int getLinkId() {
		return linkId;
	}

	public void setLinkId(int linkId) {
		this.linkId = linkId;
	}

	public int length() {
		return content.length();
	}

	public char charAt(int arg0) {
		return content.charAt(arg0);
	}

	public TextFragment subSequence(int from, int to) {
		TextFragment ret = new TextFragment(content, from, to);
		ret.fontId = fontId;
		ret.size = size;
		ret.color = color;
		ret.misc = misc;
		ret.linkId = linkId;
		return ret;
	}

	public boolean isSameStyleWith(int fontId, int size, int color, int misc,
			int linkId) {
		return this.fontId == fontId && //
				this.size == size && //
				this.color == color && //
				this.misc == misc && //
				this.linkId == linkId;
	}

	public boolean isSameStyleWith(TextFragment other) {
		// 照顾到没有JIT的杯具机型，这里就不重用上面的判断函数了…………
		return this.fontId == other.fontId && //
				this.size == other.size && //
				this.color == other.color && //
				this.misc == other.misc && //
				this.linkId == other.linkId;
	}

	int idx;

	public char first() {
		idx = 0;
		return current();
	}

	public char last() {
		idx = content.length() - 1;
		if (idx < 0) {
			idx = 0;
		}
		return current();
	}

	public char current() {
		return content.charAt(idx);
	}

	public char next() {
		idx++;
		if (idx >= content.length()) {
			idx = content.length();
			return DONE;
		}
		return current();
	}

	public char previous() {
		idx--;
		if (idx < 0) {
			idx = 0;
			return DONE;
		}
		return current();
	}

	public char setIndex(int position) {
		if (position < 0 || position > content.length()) {
			throw new IllegalArgumentException();
		}
		idx = position;
		if (idx == content.length()) {
			return DONE;
		}
		return current();
	}

	public int getBeginIndex() {
		return 0;
	}

	public int getEndIndex() {
		return content.length();
	}

	public int getIndex() {
		return idx;
	}

	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			return null;
		}
	}

	public String toString() {
		return content.toString();
	}

}
